package com.tensquare.notice.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.notice.pojo.TbNotice;
import com.tensquare.notice.pojo.TbNoticeFresh;
import com.tensquare.notice.service.impl.TbNoticeServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 文章 前端控制器
 * </p>
 *
 * @author XXX
 * @since 2022-11-09
 */
@RestController
@RequestMapping("/notice")
@CrossOrigin
public class TbNoticeController {
    
    @Autowired
    private TbNoticeServiceImpl noticeService;
    
    @ApiOperation(value = "删除待推送消息（新消息）")
    @DeleteMapping("/fresh")
    public Result freshDelete(@RequestBody TbNoticeFresh noticeFresh){
        noticeService.freshDelete(noticeFresh);
        return new Result(true,StatusCode.OK,"删除成功",null);
    }
    
    @ApiOperation(value = "根据用户Id来查询该用户的待推送消息（新消息）")
    @PostMapping("/fresh")
    public Result freshPage(@RequestParam String userId,@RequestParam Integer page,@RequestParam Integer size){
        Page<TbNoticeFresh> pageData= noticeService.freshPage(userId,page,size);
        PageResult<TbNoticeFresh> pageResult = new PageResult<>(pageData.getTotal(), pageData.getRecords());
        return new Result(true,StatusCode.OK, "查询成功",pageResult);
    }  
    
    @ApiOperation(value = "修改通知")
    @PutMapping()
    public Result updpateById(@RequestBody TbNotice notice){
        noticeService.updateById(notice);
        return new Result(true,StatusCode.OK,"修改成功",null);
    }
    
    @ApiOperation(value = "新增通知和提醒新通知")
    @PostMapping()
    public Result save(@RequestBody TbNotice notice){
        noticeService.saveNoticeAndNoticeFresh(notice);
        return new Result(true,StatusCode.OK,"新增成功",null);
    }
    
    @ApiOperation(value = "根据条件分页查询消息通知")
    @PostMapping("/search/{page}/{size}")
    public Result selectByList(@RequestBody TbNotice notice, @PathVariable Integer page, @PathVariable Integer size){

        if (notice!=null){
            Page<TbNotice> pageData= noticeService.selectByList(notice,page,size);
            PageResult<TbNotice> pageResult = new PageResult<>(pageData.getTotal(), pageData.getRecords());
            return new Result(true,StatusCode.OK, "查询成功",pageResult);
        }
        return new Result(false,StatusCode.ERROR, "查询失败",null);
    }
    
    @ApiOperation(value = "根据消息Id查询消息通知")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable String id){
         TbNotice notice= noticeService.selectById(id);
         return new Result(true,StatusCode.OK,"查询成功",notice);
    }

}
